package com.adprof.demo.utils;


import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.adprof.demo.android.R;
import com.adprof.demo.main.MainActivity;

public class DialogActivityDemo extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_permission);
        SharedPreferences sharedPreferences = getSharedPreferences("permission", MODE_PRIVATE);
        boolean isFirst = sharedPreferences.getBoolean("isFirst", true);
        if (!isFirst) {
            Intent intent = new Intent(DialogActivityDemo.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
        findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.disagreeBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.agreeBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("permission", MODE_PRIVATE);
                sharedPreferences.edit().putBoolean("isFirst", false).apply();
                Intent intent = new Intent(DialogActivityDemo.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        TextView content = findViewById(R.id.content);
        String fullText = content.getText().toString();
        SpannableStringBuilder builder = new SpannableStringBuilder(fullText);
        
        // 设置隐私政策链接（定位到文本中的"隐私政策"部分）
        int policyStart = fullText.indexOf("隐私政策");
        if (policyStart != -1) {
            int policyEnd = policyStart + "隐私政策".length();
            builder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    // 打开本地隐私政策文件
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file:///android_asset/adprof-privacy-policy.html"), "text/html");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        startActivity(intent);
                    } catch (Exception e) {
                        // 如果无法打开，则使用WebView显示
                        Intent webIntent = new Intent(DialogActivityDemo.this, WebViewActivity.class);
                        webIntent.putExtra("url", "file:///android_asset/adprof-privacy-policy.html");
                        startActivity(webIntent);
                    }
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(Color.BLUE);
                    ds.setUnderlineText(true);
                }
            }, policyStart, policyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        content.setText(builder);
        content.setMovementMethod(LinkMovementMethod.getInstance());

    }
}
